package fragment;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator;
import android.widget.Button;
import android.widget.ImageView;

import com.example.administrator.animation_demo.R;

/**
 * Created by eyesee on 2017/5/8.
 */

public class TranslateFragment extends Fragment implements View.OnClickListener {

    private View mView;
    private Button mBtstart;
    private Button mBtstop;
    private ImageView mIv;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mView = View.inflate(getActivity(), R.layout.fragment_tanslate, null);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        initView(mView);
        return mView;
    }


    private void initView(View view) {
        mBtstart = (Button) view.findViewById(R.id.tr_start);
        mBtstop = (Button) view.findViewById(R.id.tr_stop);
        mIv = (ImageView) view.findViewById(R.id.iv);

        mBtstart.setOnClickListener(this);
        mBtstop.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tr_start:
                SAnimate(mIv);
                break;
            case R.id.tr_stop:
                OAnimate(mIv);
                break;
        }
    }

    public void SAnimate(View view){
        ObjectAnimator animator1 = ObjectAnimator.ofFloat(view,"translationX",0,200,-200,0);
        ObjectAnimator animator2 = ObjectAnimator.ofFloat(view,"translationY",0,200,-200,0);
        AnimatorSet set = new AnimatorSet();
        set.play(animator1).with(animator2);
        set.setInterpolator(new LinearInterpolator());
        set.setDuration(5000);
        set.start();
    }

    public void OAnimate(View view){
        ObjectAnimator animator1 = ObjectAnimator.ofFloat(view,"translationX",200,0);
        ObjectAnimator animator2 = ObjectAnimator.ofFloat(view,"translationY",200,0);
        AnimatorSet set = new AnimatorSet();
        set.play(animator1).with(animator2);
        set.setDuration(5000);
        set.start();
    }
    /**
     * TranslateAnimation
     * 以view所在位置为原点进行平移,x轴向右为正方向,y轴向下为正方向
     * float参数为平移的距离,单位是px
     *
     * */
}
